<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .title {
            font-size: 30px;
        }
        body{
            width: 100%;
            height: 100%;
            background-color: yellow;
        }
        #myDiv{
            background-color: red;
        }
    </style>
</head>
<body>
<div class="title">h5页面</div>
<button onclick="sendMessage()">发送消息给原生</button>
<div id="myDiv"></div>
</body>
</html>

<script>
    let currenPort
    window.addEventListener("message", (event) => {
        if (event.data == "trans_port") {
            currenPort = event.ports[0]
            
            currenPort.onmessage = (event) => {
                document.querySelector("#myDiv").innerHTML = event.data
            }
        }
    })

    function sendMessage(){
        var obj = {"type":"alert", "message":"我是网页中的获取的数据"}
        currenPort.postMessage(JSON.stringify(obj))
    }


    
</script>